// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.process;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.dengues.core.components.NodePropertyParameter;
import org.dengues.core.metadata.MetadataTable;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;

/**
 * qzhang class global comment. Detailled comment <br/>
 * 
 * 
 */
public abstract class CompElement implements Cloneable, Serializable {

    public abstract Point getLocation();

    public abstract Dimension getSize();

    private List<NodePropertyParameter> elementParameters;

    private List<MetadataTable> metadataList = new ArrayList<MetadataTable>();

    // property change listeners
    protected PropertyChangeSupport listeners = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        listeners.addPropertyChangeListener(listener);
    }

    protected void firePropertyChange(String prop, Object old, Object newValue) {
        listeners.firePropertyChange(prop, old, newValue);
    }

    protected void fireStructureChange(String prop, Object child) {
        listeners.firePropertyChange(prop, null, child);
    }

    // implemented in order to create listeners field
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        listeners = new PropertyChangeSupport(this);
    }

    public void removePropertyChangeListener(PropertyChangeListener l) {
        listeners.removePropertyChangeListener(l);
    }

    public abstract String getCompName();

    public List<NodePropertyParameter> getElementParameters() {
        return elementParameters;
    }

    public void setElementParameters(List<NodePropertyParameter> elementParameters) {
        this.elementParameters = elementParameters;
    }

    public NodePropertyParameter getElementParameter(String name) {
        for (NodePropertyParameter parameter : elementParameters) {
            if (name.equals(parameter.getName())) {
                return parameter;
            }
        }
        return null;
    }

    public void setNodeParameterValue(String name, Object value) {
        NodePropertyParameter elementParameter = getElementParameter(name);
        if (elementParameter != null) {
            elementParameter.setValue(value);
        }
    }

    /**
     * Getter for metadataList.
     * 
     * @return the metadataList
     */
    public List<MetadataTable> getMetadataList() {
        return this.metadataList;
    }

    /**
     * Sets the metadataList.
     * 
     * @param metadataList the metadataList to set
     */
    public void setMetadataList(List<MetadataTable> metadataList) {
        this.metadataList = metadataList;
    }

    public abstract ICompProcess getCompProcess();

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        String s = getCompName();
        s = s + ":"; //$NON-NLS-1$
        return s;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#clone()
     */
    @Override
    public Object clone() {
        Object object = null;
        try {
            object = super.clone();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }
}
